#include "stm32f10x.h"
#include "stdio.h"
#include "FreeRTOS.h"
#include "task.h"
#include "lwprintf.h"
#include "lwbtn.h"
#include "stm32f10x.h"
#include "usart.h"
#include "dht11.h"
#include "SysTick.h"
#include "led.h"

#define BTN_PIN         GPIO_Pin_13
#define BTN_PORT        GPIOC
#define BTN_RCC_CLK     RCC_APB2Periph_GPIOC


#define BTN_PIND         GPIO_Pin_13
#define BTN_PORTD        GPIOD
#define BTN_RCC_CLKD     RCC_APB2Periph_GPIOD



float temp, humi;
uint8_t lwbtn_keys;


typedef struct {
    GPIO_TypeDef* port;
    uint16_t      pin;
    uint8_t       active_level;   /* 0：低电平视为按下  1：高电平视为按下 */
} btn_pin_cfg_t;



static btn_pin_cfg_t key1_cfg = { BTN_PORT, BTN_PIN, 0 };
static btn_pin_cfg_t key2_cfg = { BTN_PORTD, BTN_PIND, 0 };

static lwbtn_btn_t buttons[] = {
    { .arg = &key1_cfg},
			
		{.arg = &key2_cfg }
		
		
};
static uint8_t get_button_state(lwbtn_t* lw, lwbtn_btn_t* btn)
{
    btn_pin_cfg_t* cfg = (btn_pin_cfg_t*)btn->arg;

    /* 读寄存器 */
    uint8_t level = (cfg->port->IDR & cfg->pin) ? 1 : 0;

    /* 与有效电平比较 */
    return (level == cfg->active_level) ? 1 : 0;
}


static void button_event(lwbtn_t* lw, lwbtn_btn_t* btn, lwbtn_evt_t evt)
{
    (void)lw; 
	  (void)btn;
    /* 这里什么也不做，或点灯、发队列均可 */
	
	    switch (evt)
    { 
			
			   /* 按键按下事件 */
        case LWBTN_EVT_ONPRESS:   
					lwprintf_printf("KEY0 DOWN\r\n");  
				break;
				 /* 按键释放事件 */
        case LWBTN_EVT_ONRELEASE: 
					lwprintf_printf("KEY0 UP\r\n");     
				break;
				   /* 单击/双击事件 */
        case LWBTN_EVT_ONCLICK:   
					//lwprintf_printf("KEY0 CLICK\r\n");  
						if (lwbtn_click_get_count(btn) == 2) {
                lwprintf_printf("Double\r\n");
            } else {
                lwprintf_printf("click\r\n");
            }
				
				
				break;
				case LWBTN_EVT_KEEPALIVE:
						printf("long preccess \n");
				break;
        default: break;
    }
}



/*-------------- GPIO 初始化 --------------*/
static void btn_gpio_init(void)
{
    RCC_APB2PeriphClockCmd(BTN_RCC_CLK, ENABLE);
    GPIO_InitTypeDef gpio = {
        .GPIO_Pin  = BTN_PIN,
        .GPIO_Mode = GPIO_Mode_IPU   // 上拉输入
    };
    GPIO_Init(BTN_PORT, &gpio);
}



int my_uart_send_char(int ch, struct lwprintf *lwobj)
{
    while (!(USART1->SR & USART_SR_TXE))
        ; // 等待上一次发送完成
    USART1->DR = (uint8_t)ch;
    return ch;
}





void LED_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    GPIO_SetBits(GPIOE, GPIO_Pin_7);
	
	
}

void vTask1(void *pvParameters)
{
    LED_GPIO_Config();

    for (;;)
    {
        lwprintf_printf("Task1 is running\n");
        LEDS_Runs();
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
void vTask2(void *pvParameters)
{

    DHT11_GPIO_Init();

    for (;;)
    {

       // lwprintf_printf("Task2 is running\n");

       /* LED_Toggle(GPIOE, GPIO_Pin_7); //
        DHT11_Read_Data();
        lwprintf_printf("temperature = %.2f\n", Get_temperature());

        lwprintf_printf("humidity = %.2f\n", Get_humidity());
			
			*/ 
			
			
			         lwbtn_process_ex(NULL, xTaskGetTickCount()); // FreeRTOS 1 ms tick


        vTaskDelay(pdMS_TO_TICKS(10)); // 等待100ms
    }
}

int main(void)
{
    SysTick_Init(72);
    USART1_Init(115200);              // 初始化串口
    lwprintf_init(my_uart_send_char); // 初始化lwprintf
    lwprintf_printf("susses\n");    // 开机打印
    LEDS_GPIO_Init(); // 初始化LED
    btn_gpio_init();

    lwbtn_init_ex(NULL, buttons, 1, get_button_state, button_event);

    xTaskCreate(vTask1, "Task1", 1024, NULL, 1, NULL); // 创建任务1
    xTaskCreate(vTask2, "Task2", 1024, NULL, 2, NULL); // 创建任务1
    vTaskStartScheduler();
}
